package com.friends.utils.json;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * <pre>
 * JsonBase64Util
 * 描述信息
 * </pre>
 *
 * @author cgy
 * @since 2025/5/13 11:50
 */
@Slf4j
@Component
public class JsonBase64Util {

//    private ObsUtil obsUtil;

    /**
     * <pre>
     * 将json格式字符串中的指定字段数据上传至obs，并且替换成obsUrl
     * </pre>
     *
     * @param json  json内容
     * @param field 字段名称
     * @return
     */
    public String compression(String json, String field) {
        try {
            JSONObject jsonObject = JSON.parseObject(json);

            replaceWithObsUrl(jsonObject, field);

            return jsonObject.toJSONString();
        } catch (Exception e) {
            log.error("上传base64到Obs异常 ", e);
            return json;
        }
    }

    /**
     * 将指定字段替换成obsUrl
     *
     * @param jsonObject JSONObject
     * @param field      字段名
     */
    private void replaceWithObsUrl(JSONObject jsonObject, String field) {
        try {
            for (String key : jsonObject.keySet()) {
                // 替换字段
                if (field.equals(key)) {
                    final String value = jsonObject.getString(key);
//                    final String replacement = obsUtil.uploadText(value, UUIDHexGenerator.generator() + ".txt", 30);
	                final String replacement = "URL";
                    jsonObject.put(key, replacement);
                    // jsonObject.put(key, "#replacement");
                    return;
                }

                final Object obj = jsonObject.get(key);
                if (Objects.isNull(obj)) {
                    return;
                }

                if (obj instanceof JSONObject) {
                    replaceWithObsUrl((JSONObject) obj, field);
                } else if (obj instanceof JSONArray) {
                    for (Object o : ((JSONArray) obj)) {
                        replaceWithObsUrl((JSONObject) o, field);
                    }
                }
            }
        } catch (Exception e) {
        }
    }

    public static void main(String[] args) {
        String json = "{\"input\":{\"apply_info\":{\"rpt_type\":0,\"rpt_no\":\"351900005888\",\"chkr\":\"盛苗苗\",\"chk_time\":\"2025-05-12 21:54:15\",\"notes\":\"\",\"source_system\":\"ZLHIS\",\"pat_name\":\"关赛\",\"chkin_time\":\"2025-05-12 14:41:23\",\"rptr\":\"叶艳君\",\"pid\":\"281680\",\"eq_id\":\"286750201835274258\",\"rpt_path\":\"[https://obs.labway.cn/labway-lims/dongguan/2025/05/12/21/54/df49119b58c9488b8dd6bf043e51893a]\",\"pvid\":\"1\",\"report_time\":\"2025-05-12 15:51:20\",\"apply_id\":\"16540861\",\"chkin_ps\":\"陈忠敏\",\"lspcm_no\":\"600060\",\"spcm_bc_no\":\"250511000563\"},\"head\":{\"bizno\":\"S4010\",\"sysno\":\"LABWAYLIS\",\"tarno\":\"ZLHIS\",\"time\":\"2025-05-12 21:56:00\",\"action_no\":\"5d01b32690f642bfa05861dfc89e9880\"},\"rpt_pic\":[],\"rpt_info\":[{\"loitem_id\":\"Z1001301\",\"micros_not_found\":\"\",\"cltr_no_bctrim\":\"\",\"combin_code\":\"\",\"oaflag\":\"2\",\"microscope\":\"\",\"cltr_desc\":\"\",\"loitem_cname\":\"总铁结合力测定\",\"apply_id\":\"16540861\",\"bctrim_normal\":\"\",\"seq\":1,\"labitem_name\":\"血清铁四项测定(TIBC、Fe、TS、UIBC)(比色法)/兰卫\",\"is_critical\":false,\"labitem_code\":\"2515157\",\"cltr_comment\":\"\",\"ast_list\":[],\"bctrim_not_found\":\"\",\"loitem_rv\":\"54-77\",\"cltr_no_deadly\":\"\",\"micros_result\":\"\",\"micros_find\":\"\",\"cltr_tiem\":\"\",\"loitem_unit\":\"umol/L\",\"order_rpt_result\":\"53.9\",\"lspcm_no\":\"600060\",\"loitem_ename\":\"\",\"cltr_supp_comment\":\"\"},{\"loitem_id\":\"Z1001201\",\"micros_not_found\":\"\",\"cltr_no_bctrim\":\"\",\"combin_code\":\"\",\"oaflag\":\"2\",\"microscope\":\"\",\"cltr_desc\":\"\",\"loitem_cname\":\"不饱和铁结合力测定\",\"apply_id\":\"16540861\",\"bctrim_normal\":\"\",\"seq\":2,\"labitem_name\":\"血清铁四项测定(TIBC、Fe、TS、UIBC)(比色法)/兰卫\",\"is_critical\":false,\"labitem_code\":\"2515157\",\"cltr_comment\":\"\",\"ast_list\":[],\"bctrim_not_found\":\"\",\"loitem_rv\":\"25.0-50.1\",\"cltr_no_deadly\":\"\",\"micros_result\":\"\",\"micros_find\":\"\",\"cltr_tiem\":\"\",\"loitem_unit\":\"umol/L\",\"order_rpt_result\":\"19.40\",\"lspcm_no\":\"600060\",\"loitem_ename\":\"\",\"cltr_supp_comment\":\"\"},{\"loitem_id\":\"Z1001401\",\"micros_not_found\":\"\",\"cltr_no_bctrim\":\"\",\"combin_code\":\"\",\"oaflag\":\"3\",\"microscope\":\"\",\"cltr_desc\":\"\",\"loitem_cname\":\"转铁蛋白饱和度\",\"apply_id\":\"16540861\",\"bctrim_normal\":\"\",\"seq\":3,\"labitem_name\":\"血清铁四项测定(TIBC、Fe、TS、UIBC)(比色法)/兰卫\",\"is_critical\":false,\"labitem_code\":\"2515157\",\"cltr_comment\":\"\",\"ast_list\":[],\"bctrim_not_found\":\"\",\"loitem_rv\":\"20-55\",\"cltr_no_deadly\":\"\",\"micros_result\":\"\",\"micros_find\":\"\",\"cltr_tiem\":\"\",\"loitem_unit\":\"%\",\"order_rpt_result\":\"64.0\",\"lspcm_no\":\"600060\",\"loitem_ename\":\"\",\"cltr_supp_comment\":\"\"},{\"loitem_id\":\"030400700101\",\"micros_not_found\":\"\",\"cltr_no_bctrim\":\"\",\"combin_code\":\"\",\"oaflag\":\"3\",\"microscope\":\"\",\"cltr_desc\":\"\",\"loitem_cname\":\"铁测定\",\"apply_id\":\"16540861\",\"bctrim_normal\":\"\",\"seq\":4,\"labitem_name\":\"血清铁四项测定(TIBC、Fe、TS、UIBC)(比色法)/兰卫\",\"is_critical\":false,\"labitem_code\":\"2515157\",\"cltr_comment\":\"\",\"ast_list\":[],\"bctrim_not_found\":\"\",\"loitem_rv\":\"7.8-32.2\",\"cltr_no_deadly\":\"\",\"micros_result\":\"\",\"micros_find\":\"\",\"cltr_tiem\":\"\",\"loitem_unit\":\"umol/L\",\"order_rpt_result\":\"34.50\",\"lspcm_no\":\"600060\",\"loitem_ename\":\"\",\"cltr_supp_comment\":\"\"}],\"rpt_file\":[{\"note\":\"\",\"file_content\":\"https://obs.labway.cn/808080819637273d0196c4c7f2ff08cc.txt\",\"file_name\":\"df49119b58c9488b8dd6bf043e51893a.pdf\",\"lspcm_no\":\"1\",\"file_format\":\"PDF\"}]}}";
        JSONObject jsonObject = JSON.parseObject(json);
        final JsonBase64Util jsonBase64Util = new JsonBase64Util();
        json = jsonBase64Util.compression(json, "file_content");

        System.out.println("json = " + json);

    }
}
